﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Telerik.Web.Mvc;
using ProjectManagement.Models;
using Projectmanagement.Utl;
using Projectmanagement.Entity;

namespace Projectmanagement.Controllers
{
    public class MilestoneController : Controller
    {
        //
        // GET: /Milestone/

        public ActionResult Index()
        {
            return View();
        }
        [GridAction]
        public ActionResult _SelectIndex(int? conferenceid, int? projectid)
        {
            return View(new GridModel(DQ.GetMilestone()));
        }
        [GridAction]
        public ActionResult _SelectIndexProject(int? conferenceid)
        {
            var task = (from x in CH.DB.Tasks
                        //where x.ProjectID == conferenceid
                        select x).Distinct().ToList();

            var milestone = DQ.GetMilestone().ToList();
            var exceptE = milestone.Where(a => !task.Select(b => b.MilestoneID).Contains(a.ID) || a.Projectid == conferenceid);

            return View(new GridModel(exceptE));
        }
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _SaveAjaxEditing(int id, int? conferenceid)
        {
            var item = CH.GetDataByID<Milestone>(id);
            if (TryUpdateModel(item))
            {
                CH.Edit<Milestone>(item);
            }
            return View(new GridModel(DQ.GetMilestone()));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _InsertAjaxEditing(int? conferenceid)
        {
            var item = new Milestone();

            if (TryUpdateModel(item))
            {
                CH.Create<Milestone>(item);
            }
            return View(new GridModel(DQ.GetMilestone()));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _DeleteAjaxEditing(int id, int? conferenceid)
        {
            CH.Delete<Milestone>(id);
            return View(new GridModel(DQ.GetMilestone()));
        }





        [GridAction]
        public ActionResult _SelectIndexMilestone(int? conferenceid)
        {
            return View(new GridModel(DQ.GetMilestoneProject(conferenceid)));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _SaveAjaxEditingMilestone(int id, int? conferenceid)
        {
            var item = CH.GetDataByID<Milestone>(id);
            if (TryUpdateModel(item))
            {
                if (item.Projectid != null)
                {
                    //var customer = CH.DB.Customer.Find(item.Customerid);
                    //if (customer != null)
                    //{
                    item.Projectid = conferenceid;
                    //}
                }
                CH.Edit<Milestone>(item);
            }
            return View(new GridModel(DQ.GetMilestoneProject(conferenceid)));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _InsertAjaxEditingMilestone(int? conferenceid)
        {
            var item = new Milestone();

            if (TryUpdateModel(item))
            {
                //var customer = CH.DB.Customer.Find(item.Customerid);
                //if (customer != null)
                //{
                item.Projectid = conferenceid;
                //}
                CH.Create<Milestone>(item);
            }
            return View(new GridModel(DQ.GetMilestoneProject(conferenceid)));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _DeleteAjaxEditingMilestone(int id, int? conferenceid)
        {
            CH.Delete<Milestone>(id);
            return View(new GridModel(DQ.GetMilestoneProject(conferenceid)));
        }
    }
}
